<template>
  <div>
    <div class="header">
      <el-button size="small" type="primary" @click="dialogVisible=true">添加</el-button>
    </div>

    <div class="content">
      <el-table
        :data="list"
        :header-cell-style="{'color':'#000'}"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="ID"
          width="100"
          align="center"
        />
        <el-table-column
          label="图片"
          width="500"
          align="center"
        >
          <template slot-scope="scope">
            <img :src="scope.row.url" style="width: 220px;height: 120px;object-fit: cover;border-radius: 10px;">
          </template>
        </el-table-column>

        <el-table-column
          label="创建时间"
          width="180"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.updated_at }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <div class="btn">
              <span @click="handleDelete(scope.$index,scope.row)">删除</span>
            </div>
          </template>
        </el-table-column>
        <div slot="empty">
          暂无数据
        </div>
      </el-table>
    </div>

    <el-dialog
      title="新增图片"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="()=>{ resetForm('ruleForm') }"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px">

        <el-form-item label="上传图片" prop="url">
          <el-upload
            class="uploadImg"
            :action="UPLOAD_URL"
            list-type="picture-card"
            name="image"
            :file-list="[]"
            :limit="1"
            :on-success="handleSuccess"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus" />
          </el-upload>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="ruleForm.sort" type="number" />
          <p class="hint">请输入数字，序号越大排序越靠前</p>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { UPLOAD_URL } from '@/env'
export default {
  data() {
    return {
      UPLOAD_URL,
      dialogVisible: false,
      ruleForm: {
        url: '',
        sort: 1
      },
      rules: {
        url: [
          { required: true, message: '请上传图片', trigger: 'blur' }
        ]
      },
      list: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取表格数据
    getList() {
      this.$request.get('/admin/banner').then(res => {
        this.list = res.data
      }).catch()
    },

    // 图片上传
    handleSuccess(res) {
      if (res.code == 200) {
        this.ruleForm.url = res.data.path
      } else {
        this.$message.error(res.msg)
      }
    },
    handleRemove() {
      this.ruleForm.url = ''
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$request.post('/admin/banner/create', this.ruleForm).then(res => {
            this.$refs[formName].resetFields()
            this.dialogVisible = false
            this.getList()
          }).catch()
        }
      })
    },

    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.dialogVisible = false
    },

    // 操作
    handleDelete(idx, row) {
      this.$confirm('此操作将删除该图片, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$request.post(`/admin/banner/delete/${row.id}`).then(_ => {
          this.list.splice(idx, 1)
          this.$message({ message: '删除成功', type: 'success' })
        }).catch()
      }).catch()
    }
  }
}
</script>

<style lang="scss" scoped>
    .header{
        width: 100%;
        height: auto;
        padding: 10px;
        background-color: #fff;
        display: flex;
        justify-content: space-between;
    }

    .content{
        width: 100%;
        margin-top: 10px;
    }

    .btn{
    &>span{
      display: inline-block;
      color: #1989fa;
      width: 40px;
      text-align: center;
      cursor: pointer;
    }
  }

  .uploadImg ::v-deep .el-upload--picture-card{
      height:120px;
      width:210px;
      line-height: 124px;
    }
    .uploadImg ::v-deep .el-upload-list--picture-card .el-upload-list__item{
      height:120px;
      width:210px;
      line-height: 124px;
    }
    .uploadImg ::v-deep .el-icon-upload-success{
      position: absolute;
      right: 15px;
      top: 0px;
    }
</style>
